using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_AllowItemCell : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_AllowItemCell
        {
            #region ObjectBinding Generate

            public GameObject commonItem { protected set; get; }
            public ExText textSelectName { protected set; get; }
            public ExText textDes { protected set; get; }
            public GameObject selected { protected set; get; }
            public ExText textName { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<GameObject>("commonItem", out var __tbv0);
                commonItem = __tbv0;
                __binding.TryGetVariableValue<ExText>("textSelectName", out var __tbv1);
                textSelectName = __tbv1;
                __binding.TryGetVariableValue<ExText>("textDes", out var __tbv2);
                textDes = __tbv2;
                __binding.TryGetVariableValue<GameObject>("selected", out var __tbv3);
                selected = __tbv3;
                __binding.TryGetVariableValue<ExText>("textName", out var __tbv4);
                textName = __tbv4;
            }


            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify

        private UIN_CostItem _costItem;
        private bool _selected;
        public bool Selected
        {
            get => _selected;
            set
            {
                _selected = value;
                ui.selected.SetActive(value);
                if (value)
                {

                }

                ui.textSelectName.SetActive(value);
                ui.textName.SetActive(!value);
            }
        }

        public long uniqueId { get; private set; }

        protected UIB_UIN_AllowItemCell ui { get; set; }

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_AllowItemCell();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            _costItem = InitChildNode<UIN_CostItem>(ui.commonItem);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        public void RefreshCell(SubmitItemData itemData, IReadOnlyList<SubmitItemData> curSelectingItems)
        {
            uniqueId = itemData.uniqueId;
            _costItem.Show(itemData.itemCid, 0);
            _costItem.SetCount(0, false);
            var itemCfg = TableCenter.item.Get(itemData.itemCid);
            ui.textName.text = itemCfg.Name;
            ui.textSelectName.text = itemCfg.Name;
            ui.textDes.text = itemCfg.Describe;
            RefreshSelectState(curSelectingItems);
        }

        private void RefreshSelectState(IReadOnlyList<SubmitItemData> curSelectingItems)
        {
            for (var i = 0; i < curSelectingItems.Count; i++)
            {
                if (uniqueId == curSelectingItems[i]?.uniqueId)
                {
                    Selected = true;
                    return;
                }
            }
            Selected = false;
        }

        protected override void OnDispose()
        {

        }
    }
}
